'use strict';

if (GetUrlParam('language') == 'ar') {
    document.getElementsByTagName('body')[0].className = 'ar_text';
}
console.log(GetUrlParam('token'), GetUrlParam('user_id'), '活动id', GetUrlParam('activity_id'), window.location, '---------------');
vueObj = new Vue({
    el: '#page',
    data: function data() {
        return {
            abc:[
              [{
                  gift_name:'11111111',
                  gift_num:'1111111111'
              }],
              [
                  {
                gift_name:'11111111',
                gift_num:'1111111111'
            },
            {
                gift_name:'11111111',
                gift_num:'1111111111'
            }
        ],
            [{
                gift_name:'11111111',
                gift_num:'1111111111'
            },
            {
                gift_name:'11111111',
                gift_num:'1111111111'
            }]
            ],
            'A':'A :',
            'B':'B :',
            'C':'C :',
            'D':'D :',
            'E':'E :',
            'F':'F :',
            'G':'G :',
             'H':'H :',
             'I':'I :',
             'J':'J :',
             'K':'K :',
             'L':'L :',
            region: '', //国家码
            imgStr: '',
            imgUrl: [

            ],
            videStr: '',
            typeab: 0,
            dFlag: false,
            typeIv: 2,
            type: [

            ],
            list: [
                ['A', 10, 80000, [8000]],
                ['B', 15, 125000, [10000]],
                ['C', 15, 250000, [20000]],
                ['D', 20, 375000, [30000]],
                ['E', 25, 625000, [50000]],
                ['F', 25, 750000, [60000]],
                ['G', 30, 875000, [70000]],
                ['H', 35, 1000000, [80000]],
                ['I', 40, 1250000, [100000]],
                ['J', 40, 1750000, [140000]],
                ['K', 40, 2500000, [200000]],
                ['L', 40, 3750000, [300000]],



            ],
            user_id: GetUrlParam('user_id') || '',
            isRule: false,
            lan: {},
            user_list: [],
            roomlist: [],
            showMsg: {},
            tabT: 1,
            // currentLan: 'hi', // tr id hi
            currentLan: 'tr', // tr id hi
            // 活动扶持开始
            supportMark: false,
            rewardSelect: 0, // 奖励选择
            videoPath: '',
            form: {
                file: ''
            },
            activeId: GetUrlParam('activity_id') || '',
            appActiceId: GetUrlParam('activity_id') || '',
            user_id: GetUrlParam('user_id') || '',
            activeData: {
                con_list: [],
                plan_info: []
            },
            enarRuleMark: false,
            trHiIdRuleMark: false,
            videoUploadUrl: '' // 添加
            // 活动扶持开始结束
        };
    },
    watch: {
        imgStr(newUrl) {
            console.log(newUrl, "连接");
            if (newUrl.indexOf('mp4') > -1) {
                this.videStr = newUrl
                console.log(this.videStr);
                return
            }
            if (this.imgUrl.length > 2) return
            this.imgUrl.push(newUrl)
            console.log('image', this.imgUrl);
        },
        //         videStr(newUrl){
        // console.log(newUrl)
        // this.videoUrl=newUrl
        // console.log(this.videoUrl);
        //         }
    },
    created: function created() {
        // this.lan= language(GetUrlParam('language')) 

        this.$nextTick(() => {
            this.type = [{
                    typeA: this.lan.a42
                },
                {
                    typeA: this.lan.a43
                }
            ]
        })
        console.log(GetUrlParam('token'), GetUrlParam('user_id'), '活动id', GetUrlParam('activity_id'), '---------------');
        var lan = this.lan = judge_lan(this.currentLan, language);
        document.title = lan.a50;
        // let mobLan = ''
        // mobLan = GetUrlParam('language'); //手机语言
        // // mobLan = 'tr';//土耳其
        // // mobLan = 'hi';//印地语
        // // mobLan = 'id';//印尼
        // // mobLan = 'en';//英语
        // let lan = language[mobLan]
        // this.lan = lan
        // console.log('手机语言', mobLan, this.lan)
  
        // type 1 礼物 2 广播卡 3 置顶卡
        if (this.currentLan === 'tr') {
            this.user_list = [{
                img: 'https://oss.chathot.me/action/2021/07/20/img1.png',
                no: '10020',
                id: '451892',
                idText: 'chinese451892',
                t: ''
            }, {
                img: 'https://oss.chathot.me/action/2021/07/20/img1.png',
                no: '10022',
                id: '47826',
                idText: 'chinese47826',
                t: ''
            }];

            this.list = [['A', 10, 50000, [{
                type: 1,
                value: 4000
            }, {
                type: 2,
                value: '*2'
            }]], ['B', 15, 100000, [{
                type: 1,
                value: 8000
            }, {
                type: 2,
                value: '*4'
            }]], ['C', 20, 200000, [{
                type: 1,
                value: 16000
            }, {
                type: 2,
                value: '*6'
            }]], ['D', 25, 300000, [{
                type: 1,
                value: 24000
            }, {
                type: 2,
                value: '*2'
            }, {
                type: 3,
                value: '*1'
            }]], ['E', 25, 400000, [{
                type: 1,
                value: 32000
            }, {
                type: 2,
                value: '*4'
            }, {
                type: 3,
                value: '*1'
            }]], ['F', 30, 500000, [{
                type: 1,
                value: 40000
            }, {
                type: 2,
                value: '*6'
            }, {
                type: 3,
                value: '*1'
            }]]];
        } else if (this.currentLan === 'id') {
            this.user_list = [{
                img: 'https://oss.chathot.me/action/2021/07/20/img1.png',
                no: '10051',
                id: '13164',
                idText: 'chinese13164',
                t: ''
            }];

            this.list = [['A', 10, 50000, [{
                type: 1,
                value: 4000
            }, {
                type: 2,
                value: '*2'
            }]], ['B', 15, 100000, [{
                type: 1,
                value: 8000
            }, {
                type: 2,
                value: '*4'
            }]], ['C', 20, 200000, [{
                type: 1,
                value: 16000
            }, {
                type: 2,
                value: '*6'
            }]], ['D', 25, 300000, [{
                type: 1,
                value: 24000
            }, {
                type: 2,
                value: '*2'
            }, {
                type: 3,
                value: '*1'
            }]], ['E', 25, 400000, [{
                type: 1,
                value: 32000
            }, {
                type: 2,
                value: '*4'
            }, {
                type: 3,
                value: '*1'
            }]], ['F', 30, 500000, [{
                type: 1,
                value: 40000
            }, {
                type: 2,
                value: '*6'
            }, {
                type: 3,
                value: '*1'
            }]]];
        } else if (this.currentLan === 'hi') {
            this.user_list = [{
                img: 'https://oss.chathot.me/action/2021/07/20/img1.png',
                no: '10072',
                id: '644254',
                idText: 'chinese644254',
                t: ''
            }];
            this.list = [['A', 10, 50000, [{
                type: 1,
                value: 4000
            }, {
                type: 2,
                value: '*2'
            }]], ['B', 15, 100000, [{
                type: 1,
                value: 8000
            }, {
                type: 2,
                value: '*4'
            }]], ['C', 20, 200000, [{
                type: 1,
                value: 16000
            }, {
                type: 2,
                value: '*6'
            }]], ['D', 25, 300000, [{
                type: 1,
                value: 24000
            }, {
                type: 2,
                value: '*2'
            }, {
                type: 3,
                value: '*1'
            }]], ['E', 25, 400000, [{
                type: 1,
                value: 32000
            }, {
                type: 2,
                value: '*4'
            }, {
                type: 3,
                value: '*1'
            }]], ['F', 30, 500000, [{
                type: 1,
                value: 40000
            }, {
                type: 2,
                value: '*6'
            }, {
                type: 3,
                value: '*1'
            }]]];
        }
        this.roomlist = [{
            avatar: 'https://oss.chathot.me/action/activityhtml/roomActivityInHiTr/page-path1-s-' + this.currentLan + '.png?v=4',
            b: 'https://oss.chathot.me/action/activityhtml/roomActivityInHiTr/page-path1-b-' + this.currentLan + '.png',
            tit: lan.k0,
            // x: [lan.k6,lan.k6_1,lan.k6_2,lan.k6_3,lan.k6_4,lan.k6_5,lan.k6_6],
            x: [lan.k6],
            t: lan.k12,
            r: lan.k18
        }, {
            avatar: 'https://oss.chathot.me/action/activityhtml/roomActivityInHiTr/page-path2-s-' + this.currentLan + '.png?v=4',
            b: 'https://oss.chathot.me/action/activityhtml/roomActivityInHiTr/page-path2-b-' + this.currentLan + '.png',

            tit: lan.k1,
            x: [lan.k7],
            t: lan.k13,
            r: lan.k19
        }, {
            avatar: 'https://oss.chathot.me/action/activityhtml/roomActivityInHiTr/page-path3-s-' + this.currentLan + '.png',
            b: 'https://oss.chathot.me/action/activityhtml/roomActivityInHiTr/page-path3-b-' + this.currentLan + '.png',

            tit: lan.k2,
            x: [lan.k8],
            t: lan.k14,
            r: lan.k20
        }, {
            avatar: 'https://oss.chathot.me/action/activityhtml/roomActivityInHiTr/page-path4-s-' + this.currentLan + '.png',
            b: 'https://oss.chathot.me/action/activityhtml/roomActivityInHiTr/page-path4-b-' + this.currentLan + '.png',

            tit: lan.k3,
            x: [lan.k9],
            t: lan.k15,
            r: lan.k21
        }, {
            avatar: 'https://oss.chathot.me/action/activityhtml/roomActivityInHiTr/page-path5-s-' + this.currentLan + '.png',
            b: 'https://oss.chathot.me/action/activityhtml/roomActivityInHiTr/page-path5-b-' + this.currentLan + '.png',

            tit: lan.k4,
            x: [lan.k10],
            t: lan.k16,
            r: lan.k22
        }, {
            avatar: 'https://oss.chathot.me/action/activityhtml/roomActivityInHiTr/page-path6-s-' + this.currentLan + '.png?v=2',
            b: 'https://oss.chathot.me/action/activityhtml/roomActivityInHiTr/page-path6-b-' + this.currentLan + '.png?v=2',

            tit: lan.k5,
            x: [lan.k11],
            t: lan.k17,
            r: lan.k23
        }];
        // 图片预加载
        for (var imgIndex = 0; imgIndex < this.roomlist.length; imgIndex++) {
            var imgItem = this.roomlist[imgIndex];
            var oImage = new Image();
            oImage.src = imgItem.b;
        }

        // 请求活动详情
        // 活动扶持开始
        if (this.activeId) {
            this.getData();
        }
        this.extData = {
            supportLevel: {
                arEn: [{
                    type: 'A',
                    num: 10,
                    gold: 80000
                }, {
                    type: 'B',
                    num: 15,
                    gold: 125000
                }, {
                    type: 'C',
                    num: 15,
                    gold: 250000
                }, {
                    type: 'D',
                    num: 20,
                    gold: 375000
                }, {
                    type: 'E',
                    num: 25,
                    gold: 625000
                }, {
                    type: 'F',
                    num: 25,
                    gold: 750000
                }, {
                    type: 'G',
                    num: 30,
                    gold: 875000
                }, {
                    type: 'H',
                    num: 35,
                    gold: 1000000
                }, {
                    type: 'I',
                    num: 40,
                    gold: 1250000
                }, {
                    type: 'J',
                    num: 40,
                    gold: 1750000
                }, {
                    type: 'K',
                    num: 40,
                    gold: 2500000
                }, {
                    type: 'L',
                    num: 40,
                    gold: 3750000
                }],
                trHiId: [{
                    type: 'A',
                    num: 10,
                    gold: 50000
                }, {
                    type: 'B',
                    num: 15,
                    gold: 100000
                }, {
                    type: 'C',
                    num: 20,
                    gold: 200000
                }, {
                    type: 'D',
                    num: 25,
                    gold: 300000
                }, {
                    type: 'E',
                    num: 25,
                    gold: 400000
                }, {
                    type: 'F',
                    num: 30,
                    gold: 500000
                }]
            }
        };
        // 活动扶持开始结束
    },
    methods: {
        typeClick(index) {
            this.typeab = index
            if (this.typeab == 0) {
                this.imgUrl = []
                this.typeIv = 2
            } else {
                this.videStr = ''
                this.typeIv = 1

            }


            //  console.log(this.imgUrl.length);
            //  console.log(this.imgUrl.length<=2);
        },
        //上传
        selectMedia(media_type) {
            if (isIphone()) {
                window.webkit.messageHandlers.selectMedia.postMessage({
                    media_type: media_type
                });
            } else {
                window.WebViewJavascriptBridge.callHandler(
                    'selectMedia', {
                        'media_type': media_type
                    },
                    function (responseData) {

                    }
                );
            }
        },
        submitChange: function submitClick() {

            var _this3 = this;
            if (this.videStr == '' && this.imgUrl.length == 0) {
                return toast(_this3.lan.a41)
            } 
            if (this.typeab=='1') {
                if(this.imgUrl.length!=3){
                    return toast(_this3.lan.a51)
               
                }
             }
            // if (!this.form.file) {
            //     return toast(this.lan.k57);
            // }
            var selectItem = this.activeData.plan_info[this.rewardSelect];
            console.log(selectItem, this.rewardSelect, this.activeData.plan_info);
            if (!selectItem || !selectItem[0] || !selectItem[0].plan_id) {
                return;
            }
            createLoad();
            var sureData = {
                "Api_name": 'activity_support',
                "Api_data": JSON.stringify({
                    // "video_url": this.videoUploadUrl,
                    "token": GetUrlParam('token'),
                    "op": "room_sup_apply",
                    "activity_id": this.activeId,
                    "plan_id": selectItem[0].plan_id,
                    "user_id": GetUrlParam('user_id'),
                    "type": this.typeIv,
                    "video_url": this.videStr == '' ? this.imgUrl.join("|") : this.videStr

                }),
                // 'thumb': this.form.file,
            };
            // console.log(sureData);
            return ajax('POST', API.api_request, sureData, {
                formDataFlag: true,
            }).then(function (res) {
                removeLoad();
                if (res.status == 200) {
                    toast(_this3.lan.k49);
                    _this3.supportMark = false;
                    _this3.getData();
                } else {
                    toast(_this3.lan.a50);
                }
            }).catch(function (err) {});
        },

        changeVideStr() {
            this.videStr = ''
        },
        //删除图片
        closeImage(index) {
            this.imgUrl = this.imgUrl.filter((item, i) => i !== index)
        },
        setTxt: function setTxt(msg) {
            this.showMsg = msg;
            this.isRule = true;
        },
        // 跳转个人主页
        go_user: function go_user(id) {
            userDetails(id);
        },
        //tab切换
        changeTab(num) {
            this.tabT = num
        },
        // 房间跳转
        roomDetails: function (_roomDetails) {
            function roomDetails(_x, _x2) {
                return _roomDetails.apply(this, arguments);
            }

            roomDetails.toString = function () {
                return _roomDetails.toString();
            };

            return roomDetails;
        }(function (id, idText) {
            // roomDetails('451334', '', 'chinese451334')
            console.log(id, '', idText);
            roomDetails(id, '', idText);
        }),
        // 活动扶持开始
        supportMarkClick: function supportMarkClick(e) {
            if (e.target.id == 'support-content') {
                this.supportMark = false;
            }
        },
        fileChange: function fileChange(e) {
            var files = e.target.files;
            var filesItem = files[0];
            if (!filesItem) return;
            var maxSize = 50 * 1024 * 1024;
            if (filesItem.size > maxSize) {
                toast(this.lan.k30);
                return;
            }
            console.log(filesItem.size, '打印size');
            console.log('打印size123');
            this.form.file = filesItem;
            var objUrl = this.getObjectURL(filesItem);
            if (objUrl) {
                this.videoPath = objUrl;
            }
        },
        //建立一个可存取到该file的url
        getObjectURL: function getObjectURL(file) {
            var url = null;
            if (window.createObjectURL != undefined) {
                // basic
                url = window.createObjectURL(file);
            } else if (window.URL != undefined) {
                // mozilla(firefox)
                url = window.URL.createObjectURL(file);
            } else if (window.webkitURL != undefined) {
                // webkit or chrome
                url = window.webkitURL.createObjectURL(file);
            }
            return url;
        },
        removeObjectURL: function removeObjectURL(ObjectFile) {
            var url = null;
            if (window.revokeObjectURL != undefined) {
                // basic
                url = window.revokeObjectURL(ObjectFile);
            } else if (window.URL != undefined) {
                // mozilla(firefox)
                url = window.URL.revokeObjectURL(ObjectFile);
            } else if (window.webkitURL != undefined) {
                // webkit or chrome
                url = window.webkitURL.revokeObjectURL(ObjectFile);
            }
            return url;
        },
        deleteVideo: function deleteVideo() {
            this.videoPath = '';
            this.videoUploadUrl = '';
            this.form.file = '';
            if (this.$refs.uploadIpt) {
                this.$refs.uploadIpt.value = '';
            }
            this.removeObjectURL(this.videoPath);
        },
        uploadVideo: function uploadVideo() {
            var _this = this;

            if (!this.form.file) {
                toast(this.lan.k53);
                return;
            }
            var query = {
                user_token: GetUrlParam('token'),
                user_id: GetUrlParam('user_id'),
                file: this.form.file
            };
            createLoad();
            return ajax2("POST", query, {
                hideLoading: false,
                formDataFlag: true
            }, '/api/Video/upVideo').then(function (res) {
                removeLoad();
                console.log(res);
                res.data = res.data || {};
                _this.videoUploadUrl = res.data.url;
                return res.data;
            }).catch(function (err) {
                removeLoad();
            });
        },
        submitClick: function submitClick() {
            var _this2 = this;

            if (this.videoUploadUrl) {
                this.handleUpload();
            } else {
                var uploadThen = this.uploadVideo();
                if (!uploadThen) {
                    return;
                }
                uploadThen.then(function (res) {
                    _this2.handleUpload();
                });
            }
        },
        handleUpload: function handleUpload() {
            var _this3 = this;

            if (!this.videoUploadUrl) {
                return toast(this.lan.k53);
            }
            var selectItem = this.activeData.plan_info[this.rewardSelect];
            console.log(selectItem, this.rewardSelect, this.activeData.plan_info);
            if (!selectItem || !selectItem[0] || !selectItem[0].plan_id) {
                return;
            }
            createLoad();
            var sureData = {
                "Api_name": 'activity_support',
                "Api_data": JSON.stringify({
                    "video_url": this.videoUploadUrl,
                    "token": GetUrlParam('token'),
                    "op": "room_sup_apply",
                    "activity_id": this.activeId,
                    "plan_id": selectItem[0].plan_id,
                    "user_id": GetUrlParam('user_id')
                })
            };
            console.log(sureData);
            return ajax('POST', API.api_request, sureData).then(function (res) {
                removeLoad();
                if (res.status == 200) {
                    toast(_this3.lan.k49);
                    _this3.supportMark = false;
                    _this3.getData();
                } else {
                    toast('Fail, ' + res.status);
                }
            }).catch(function (err) {});
        },

        getData: function getData() {
            var _this4 = this;

            createLoad();
            var sureData = {
                "Api_name": 'activity_support',
                "Api_data": JSON.stringify({
                    "token": GetUrlParam('token'),
                    "op": "get_room_active_info",
                    "activity_id": this.activeId,
                    "user_id": GetUrlParam('user_id')
                })
            };
            return ajax('POST', API.api_request, sureData).then(function (res) {
                console.log(res);
                removeLoad();
                res.data = res.data || {};
                res.data.con_list = res.data.con_list || [];
                res.data.plan_info = res.data.plan_info || [];
                console.log(res.data);

                var typeStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
                var resultType = [];

                for (var i = 0; i < res.data.plan_info.length; i++) {
                    var selfPrice = 0;
                    var planItem = res.data.plan_info[i];
                    for (var j = 0; j < planItem.length; j++) {
                        selfPrice = selfPrice * 1 + (res.data.plan_info[i][j].gold_price * 1 * (res.data.plan_info[i][j].gift_num * 1 || 0));
                    }
                    res.data.plan_info[i][0].selfPrice = selfPrice;
                }
                _this4.activeData = res.data;

                var levelType = _this4.handleLevelType();
                if (!levelType) {
                    _this4.activeData.plan_info = [];
                } else {
                    for (var _i = 0; _i < res.data.plan_info.length; _i++) {
                        var _planItem = res.data.plan_info[_i];
                        if (_planItem[0] && _planItem[0].level == typeStr.indexOf(levelType) + 1) {
                            resultType.push(_planItem);
                        }
                    }
                    _this4.activeData.plan_info = resultType;
                }

                if (res.status == 200) {};
            }).catch(function (err) {
                removeLoad();
            });
        },
        supportBtnClick: function supportBtnClick() {
            this.supportMark = true;
            // 已经申请了这个了
            if (this.activeData.apply_status == 1 || this.activeData.apply_status == 3 || this.activeData.apply_status == 4) return;
            // 是否在活动结束后的七天内
            var endTime = this.activeData.end_time;
            var nowTime = this.activeData.now_time;
            // 超过七天 或 活动还没结束
            if (nowTime - endTime >= 3600 * 24 * 7 || nowTime - endTime < 0) {
                toast(this.lan.k40);
                return;
            }
            // 当前房间 该房间有待发扶持 即已经申请过了还在审批，然后又申请了一次
            if (this.activeData.room_status == 1) {
                toast(this.lan.k41);
                return;
            }
            // 此房间当前是否有其他活动正在申请扶持
            if (this.activeData.week_count > 2 && (this.activeData.apply_status == 1 || this.activeData.apply_status == 3)) {
                toast(this.lan.k41);
                return;
            }
            // 每个区的固定规则
            if (this.currentLan === 'tr' || this.currentLan === 'id' || this.currentLan === 'hi') {
                if (!(this.activeData.week_count < 2 && this.activeData.count >= 10 && this.activeData.number >= 10 && this.activeData.total_gold >= 50000)) {
                    // 弹窗
                    this.trHiIdRuleMark = true;
                    return;
                }
            } else {
                if (!(this.activeData.week_count < 2 && this.activeData.count >= 10 && this.activeData.number >= 20 && this.activeData.total_gold >= 80000)) {
                    // 弹窗
                    this.enarRuleMark = true;
                    return;
                }
            }
            this.supportMark = true;
        },
        handleLevelType: function handleLevelType() {
            var count = this.activeData.count;
            var total_gold = this.activeData.total_gold;
            var awardArr = [];
            if (this.currentLan === 'tr' || this.currentLan === 'id' || this.currentLan === 'hi') {
                awardArr = this.extData.supportLevel.trHiId;
            } else {
                awardArr = this.extData.supportLevel.arEn;
            }
            for (var i = awardArr.length - 1; i >= 0; i--) {
                var item = awardArr[i];
                if (count >= item.num && total_gold >= item.gold) {
                    return item.type;
                }
            }
            return '';
        },
        rewardItemClick: function rewardItemClick(index) {
            this.rewardSelect = index;
        }
        // 活动扶持开始结束
    },
    mounted: function mounted() {
        var height = document.documentElement.clientHeight || document.body.clientHeight;
        var page = document.querySelector('#page');
        page.style.minHeight = height + 'px';
    }

});